﻿using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Nextensions.GUI.Extensions
{
    public static class RtfExtension
    {
        private static readonly Regex _line = new Regex(@"\n", RegexOptions.Compiled);

        public static void WriteError(this RichTextBox RtfTextBox, string Error)
        {
            Write(ref RtfTextBox, Error, Color.Red);
        }

        public static void WriteErrorF(this RichTextBox RtfTextBox, string Format, params object[] Args)
        {
            WriteError(RtfTextBox, String.Format(Format, Args));
        }

        public static void WriteWarning(this RichTextBox RtfTextBox, string Error)
        {
            Write(ref RtfTextBox, Error, Color.Yellow);
        }

        public static void WriteWarningF(this RichTextBox RtfTextBox, string Format, params object[] Args)
        {
            WriteWarning(RtfTextBox, String.Format(Format, Args));
        }

        public static void WriteSuccess(this RichTextBox RtfTextBox, string Error)
        {
            Write(ref RtfTextBox, Error, Color.Lime);
        }

        public static void WriteSuccessF(this RichTextBox RtfTextBox, string Format, params object[] Args)
        {
            WriteSuccess(RtfTextBox, String.Format(Format, Args));
        }

        public static void WriteDebug(this RichTextBox RtfTextBox, string Error)
        {
            Write(ref RtfTextBox, Error, Color.LightSalmon);
        }

        public static void WriteDebugF(this RichTextBox RtfTextBox, string Format, params object[] Args)
        {
            WriteDebug(RtfTextBox, String.Format(Format, Args));
        }

        public static void WriteVerbose(this RichTextBox RtfTextBox, string Error)
        {
            Write(ref RtfTextBox, Error, Color.Gray);
        }

        public static void WriteVerboseF(this RichTextBox RtfTextBox, string Format, params object[] Args)
        {
            WriteVerbose(RtfTextBox, String.Format(Format, Args));
        }

        public static void Write(this RichTextBox RtfTextBox, string message, Color color)
        {
            Write(ref RtfTextBox, message, color);
        }

        public static void Write(ref RichTextBox RtfTextBox, string message, Color color)
        {
            int selStart = RtfTextBox.Text.Length;
            RtfTextBox.AppendText(message);
            RtfTextBox.Select(selStart, message.Length);
            RtfTextBox.SelectionColor = color;
        }

        public static int GetLineFromIndex(this RichTextBox TextBox, int Index)
        {
            return _line.Matches(TextBox.Text.Substring(0, Index)).Count;
        }

        public static int GetLineFromPoint(this RichTextBox TextBox, Point Location)
        {
            Graphics g = Graphics.FromHwnd(TextBox.Handle);
            float textHeight = TextBox.Font.GetHeight();

            Location.Offset(TextBox.AutoScrollOffset);

            return (int)Math.Round((float)Location.Y / textHeight);
        }
    }
}
